package com.heima.article.service.impl;

import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleCreateHtmlService;
import com.heima.model.article.Dtos.ArticleDto;
import com.heima.model.article.Dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author liu
 */
@Slf4j
@Service
@Transactional
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    //注入文章配置
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    //注入文章内容
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    //注入文章静态文件生成
    @Autowired
    private ArticleCreateHtmlService articleCreateHtmlService;


    //增加条件查询文章的列表数据
    //dto:传递的查询参数
    //type: 查询类型: 1 查询更多 2 查询最新消息
    @Override
    public ResponseResult load(ArticleHomeDto dto, byte type) {
        List<ApArticle> list = apArticleMapper.load(dto, type);

        return ResponseResult.okResult(list);
    }

    //保存app端相关文章
    //@Async //开启异步调用
    @Override
    public ResponseResult saveArticle(ArticleDto dto) throws TemplateException, IOException {


        //例行检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //因为传过来的参数是dto类型的 文章的保存是ApArticle类型的 所以需要copy一下复制为aparticle类型的
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        //判断是否存在id 存在就是更新 没有就是新增
        if (null == dto.getId()) {
            //保存文章
            apArticleMapper.insert(apArticle);
            //保存更新配置
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setIsDown(Boolean.FALSE);
            apArticleConfig.setIsForward(Boolean.TRUE);
            apArticleConfig.setIsDelete(Boolean.FALSE);
            apArticleConfig.setIsComment(Boolean.FALSE);
            apArticleConfigMapper.insert(apArticleConfig);
            //保存 文章内容
            ApArticleContent articleContent = new ApArticleContent();
            articleContent.setArticleId(apArticle.getId());
            articleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(articleContent);

        }else {
            // else 2.2 存在id   修改  文章  文章内容
            //修改  文章
             apArticleMapper.updateById(apArticle);
            //修改文章内容,先将原内容查询根据id查出来再设置更新为新内容
            ApArticleContent articleContent = apArticleContentMapper
                    .selectOne(Wrappers.<ApArticleContent>lambdaQuery()
                    .eq(ApArticleContent::getArticleId, apArticle.getId()));//dto.getId();
            articleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(articleContent);

        }
        //异步调用生成静态页面
        articleCreateHtmlService.ArticleTOMinIo(apArticle.getId());
        //3.结果返回  文章的id
        return ResponseResult.okResult(apArticle.getId());
    }
}
